﻿
@{
    Layout = "";
}
<style>
    .ngdialog.ngdialog-theme-plain .ngdialog-content {
        width: 45% !important;
    }
</style>


<div ng-controller="BuildingDetailController">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <form name="formValidate" ng-submit="submitForm()" novalidate="" class="form-validate form-horizontal">
                    <!-- START panel-->
                    <div class="panel panel-default" ng-class="{ 'whirl standard': isLoading,'': isLoading }">
                        <div class="panel-body">
                            <fieldset class="b0">
                                <legend>{{Building.Id == 0 || Building.Id == null ? '添加宿舍楼' : '修改宿舍楼信息'}}</legend>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">大楼号</label>
                                    <div class="col-sm-4">
                                        <input type="text" name="BuildingNO" id="BuildingNO" required="" ng-model="Building.BuildingNO" class="form-control" maxlength="2" />
                                        <span ng-show="validateInput('BuildingNO', 'required')" class="text-danger">请输入大楼号</span>
                                        <span ng-show="IsRightBuildingNO" class="text-danger">大楼号只能为数字</span>
                                        <span ng-show="IsCheckBuildingNO" class="text-danger">输入大楼号已经存在</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">地理位置</label>
                                    <div class="col-sm-4">
                                        <input type="text" name="Address" required="" ng-model="Building.Address" class="form-control" />
                                        @*<span ng-show="validateInput('Address', 'required')" class="text-danger">请输入单元数</span>
                                        <span ng-show="IsRightUnitNumber" class="text-danger">单元数只能为数字</span>*@
                                    </div>
                                    @*<div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>*@
                                </div>
                            </fieldset>
                            @*<fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">楼层数</label>
                                    <div class="col-sm-4">
                                        <input type="text" name="FloorNumber" required="" ng-model="Building.FloorNumber" class="form-control" maxlength="3"  />
                                        <span ng-show="validateInput('FloorNumber', 'required')" class="text-danger">请输入楼层数</span>
                                        <span ng-show="IsRightFloorNumber" class="text-danger">楼层数只能为数字</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">房间数</label>
                                    <div class="col-sm-4">
                                        <input type="text" name="RoomNumber" ng-model="Building.RoomNumber" class="form-control" maxlength="4" />
                                        <span ng-show="validateInput('RoomNumber', 'required')" class="text-danger">请输入房间数</span>
                                        <span ng-show="IsRightRoomNumber" class="text-danger">房间数只能为数字</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>*@
                        </div>
                        <div class="panel-footer text-right">
                            <button type="button" class="btn btn-warning" ng-click="cancel()">返回列表</button>
                            <button type="button" class="btn btn-primary" ng-click="save()">保存</button>
                        </div>
                        
                    </div>
                    <!-- END panel-->
                </form>
            </div>
        </div>
    </div>    
</div>

<div id='container' ng-show="false"></div>

<link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css" />
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.0&key=56aaa1c52d140b9b3276d2893997b0ba"></script>
<script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>

<script type="text/javascript">
    App.controller('BuildingDetailController', ["$scope", "$state", "$http", "$stateParams", "Notify", "$filter",
        function ($scope, $state, $http, $stateParams, Notify, $filter) {
            'use strict';
            $scope.isLoading = true;
            
            $scope.IsRightBuildingNO = false;
            $scope.IsRightUnitNumber = false;
            $scope.IsRightFloorNumber = false;
            $scope.IsRightRoomNumber = false;
            $scope.IsCheckBuildingNO = false;
            $scope.Longitude = 0;
            $scope.Latitude = 0;

            $scope.validateInput = function (name, type) {
                var input = $scope.formValidate[name];
                return (input.$dirty || $scope.submitted) && input.$error[type];
            };

            //数据初始化
            $http.post("/Building/GetBuildingInitDetail", { id: $stateParams.id }).then(function (response) {
 
                $scope.Building = angular.copy(response.data.buindingInfo);
                if ($scope.Building == null) {
                    $scope.Building = {};
                    
                }
                $scope.isLoading = false;
            });
            
            // 保存点击
            $scope.save = function () {
                $scope.submitted = true;
                //判断大楼号
                var testBuildingNO = /^[0-9]*$/;

                if (testBuildingNO.test($scope.Building.BuildingNO) === false) {
                    $scope.IsRightBuildingNO = true;
                    var txt = document.getElementById('BuildingNO')
                    txt.focus();
                    txt.select();
                    return false;
                } else {
                    $scope.IsRightBuildingNO = false;
                }

                //检查大楼是否存在
                $http.post("/Building/BuildingCheck", { BuildingNO: $scope.Building.BuildingNO }).then(function (returnData) {
                    if (returnData.data.isSuccess) {
                        
                        $scope.IsCheckBuildingNO = false;

                        $scope.isLoading = true;
                        if ($scope.formValidate.$valid) {
                            $http.post("/Building/BuildingSave", $scope.Building).then(function (returnData) {
                                if (returnData.data.isSuccess) {
                                    Notify.alert('操作成功!', { status: 'success' });
                                    $state.go("app.buildinglist");
                                } else {
                                    Notify.alert(returnData.data.msg, { status: 'warning' });
                                    $scope.isLoading = false;
                                }
                            });

                        } else {
                            $scope.isLoading = false;
                        }
                    } else {
                        $scope.IsCheckBuildingNO = true;
                        var txt = document.getElementById('BuildingNO')
                        txt.focus();
                        txt.select();
                        return false;
                    }
                });
                                
                
            };

            //地址定位获取地址
            $scope.getAddr = function () {
                var mapObj = new AMap.Map('container');
                mapObj.plugin('AMap.Geolocation', function () {
                    var geolocation = new AMap.Geolocation({
                        enableHighAccuracy: true,//是否使用高精度定位，默认:true
                        timeout: 10000,          //超过10秒后停止定位，默认：无穷大
                        maximumAge: 0,           //定位结果缓存0毫秒，默认：0
                        convert: true,           //自动偏移坐标，偏移后的坐标为高德坐标，默认：true
                        showButton: true,        //显示定位按钮，默认：true
                        buttonPosition: 'LB',    //定位按钮停靠位置，默认：'LB'，左下角
                        buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
                        showMarker: true,        //定位成功后在定位到的位置显示点标记，默认：true
                        showCircle: true,        //定位成功后用圆圈表示定位精度范围，默认：true
                        panToLocation: true,     //定位成功后将定位到的位置作为地图中心点，默认：true
                        zoomToAccuracy: true      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
                    });
                    mapObj.addControl(geolocation);
                    geolocation.getCurrentPosition();
                    AMap.event.addListener(geolocation, 'complete', function (data) {
                        $scope.Longitude = data.position.getLng();
                        $scope.Latitude = data.position.getLat();
                        //获取经度纬度后改
                        

                    });//返回定位信息
                    AMap.event.addListener(geolocation, 'error', function (data) {
                        console.log('定位失败');
                    });      //返回定位出错信息
                });
                
            };

            //返回列表
            $scope.cancel = function () {
                $state.go("app.buildinglist");
            };

        }]);
</script>
